using System.Collections.Generic;
using System.Text;

namespace UnitTestUtils.UI
{
    /// <summary>
    /// Allow capturing all databinding errors in UnitTest.
    /// </summary>
    /// <example>
    /// DataBindingListener listener = new DataBindingListener();
    /// System.Diagnostics.PresentationTraceSources.DataBindingSource.Listeners.Add(listener);
    /// 
    /// Assert.AreEqual(0, listener.ReportedErrors.Count);
    /// </example>
    public class DataBindingListener : System.Diagnostics.TraceListener
    {
        List<string> _reportedErrors = new List<string>();

        /// <summary>
        /// List of reported binding errors.
        /// </summary>
        public List<string> ReportedErrors
        {
            get
            {
                return _reportedErrors;
            }
        }

        /// <summary>
        /// Returns true, if binding errors have been reported.
        /// </summary>
        public bool HasErrors
        {
            get
            {
                return _reportedErrors.Count > 0;
            }
        }

        /// <summary>
        /// Generates an ErrorReport.
        /// </summary>
        /// <returns></returns>
        public string Dump()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("Reported DataBinding error:  {0}", _reportedErrors.Count);
            sb.AppendLine();
            sb.AppendLine();

            foreach (var item in _reportedErrors)
            {
                sb.AppendLine("   " + item);
            }
            sb.AppendLine();
            return sb.ToString();
        }


        #region TraceListener Implementation
        /// <summary>
        /// <see cref="System.Diagnostics.TraceListener.TraceEvent(System.Diagnostics.TraceEventCache, string, System.Diagnostics.TraceEventType, int)"/>
        /// </summary>
        /// <param name="eventCache"></param>
        /// <param name="source"></param>
        /// <param name="eventType"></param>
        /// <param name="id"></param>
        /// <param name="format"></param>
        /// <param name="args"></param>
        public override void TraceEvent(System.Diagnostics.TraceEventCache eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, string format, params object[] args)
        {
            _reportedErrors.Add(string.Format(format, args));
            base.TraceEvent(eventCache, source, eventType, id, format, args);
        }

        /// <summary>
        /// <see cref="System.Diagnostics.TraceListener.Write(string)"/>
        /// </summary>
        /// <param name="message"></param>
        public override void Write(string message)
        {
        }

        /// <summary>
        /// <see cref="System.Diagnostics.TraceListener.WriteLine(string)"/>
        /// </summary>
        /// <param name="message"></param>
        public override void WriteLine(string message)
        {
        }
        #endregion
    }
}
